const Storage = (function () {

	function setStorage(key, value) {
		return uni.setStorage({
			key: key,
			data: value
		})
	}

	function setStorageSync(key, value) {
		try {
			uni.setStorageSync(key, value)
		} catch (e) {

		}
	}

	function getStorage(key) {
		return new Promise((resolve, reject) => {
			uni.getStorage({
				key: key
			}).then(res => {
				console.log('getStorage', res);
				resolve(res)
			}).catch(error => {
				reject(error)
			})
		})
	}

	function getStorageSync(key) {
		try {
			var value = uni.getStorageSync(key)
			return value;
		} catch (error) {
			console.error('getStorageSync', error);
			return '';
		}
	}

	function getStorageInfo() {
		return uni.getStorageInfo()
	}

	function getStorageInfoSync() {
		try {
			var res = uni.getStorageInfoSync()
			return res;
		} catch (error) {
			return error;
		}
	}

	function removeStorage(key) {
		uni.removeStorage({
			key: key
		}).then(res => {
			console.log('remove storage', res)
		})
	}

	function removeStorageSync(key) {
		try {
			uni.removeStorageSync(key)
		} catch (e) {
			console.log('remove storage error', e)
		}
	}

	return {
		set: function (key, value) {
			return setStorage(key, value);
		},
		setSync: function (key, value) {
			return setStorageSync(key, value);
		},
		get: function (key) {
			return new Promise(async (resolve, reject) => {
				try {
					let data = await getStorage(key);
					resolve(data);
				} catch (error) {
					reject(error)
				}
			})
		},
		getSync: function (key) {
			return getStorageSync(key);
		},
		getAll: function () {
			return getStorageInfo();
		},
		getAllSync: function () {
			return getStorageInfoSync()
		},
		remove: function (key) {
			return removeStorage(key)
		},
		removeSync: function (key) {
			return removeStorageSync(key)
		}
	}

})()

export default Storage;